package com.ruoqing.auth.config;

import com.ruoqing.auth.service.impl.UserDetailsServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


/**
	* @author YaoXian
	* @title: WebSecurityConfiguration
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 认证授权中心 - 服务器安全配置
	* @date 2020-11-25 18:27
	*/


@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
		
		@Bean
		public BCryptPasswordEncoder passwordEncoder() {
				// 设置默认的加密方式
				return new BCryptPasswordEncoder();
		}
		
		@Bean
		@Override
		public UserDetailsService userDetailsService() {
				return new UserDetailsServiceImpl();
		}
		
		@Override
		protected void configure(AuthenticationManagerBuilder auth) throws Exception {
				// 使用自定义认证与授权
				auth.userDetailsService(userDetailsService());
		}
		
		/**
			* 将校验Token令牌和注销Token端点放行,其它所有请求都要鉴权
			* @param web
			*/
		@Override
		public void configure(WebSecurity web) {
				web.ignoring().antMatchers("/oauth/check_token").and()
				.ignoring().antMatchers("/auth/logout");
		}
		
		/**
			* 这个配置 是需要在密码模式下,注入的时候需要的这个bean
			* @return
			* @throws Exception
			*/
		@Override
		@Bean
		public AuthenticationManager authenticationManagerBean() throws Exception {
				return super.authenticationManagerBean();
		}
		
}